local erdao = fk.CreateSkill {
  name = "th_erdao",
}

erdao:addEffect(fk.EventPhaseProceeding, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(erdao.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return #p:getCardIds("ej") ~= 0
    end)
    if #targets ~= 0 then
      local to = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#th_erdao-choose",
        skill_name = erdao.name,
        cancelable = true
      })
      if #to == 1 then
        local card = room:askToChooseCard(player, {
          target = to[1],
          flag = "ej",
          skill_name = erdao.name
        })
        event:setCostData(self, { tos = to, cards = {card} })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local tos, cards = dat.tos, dat.cards
    room:obtainCard(player, cards, false, fk.ReasonPrey, player.id)
    if tos[1] == player then
      room:askToUseVirtualCard(player, {
        name = "slash",
        subcards = cards,
        skill_name = erdao.name,
        prompt = "#th_erdao-slash:::"..Fk:getCardById(cards[1]):toLogString(),
        cancelable = false,
        extra_data = {
          bypass_times = true,
        }
      })
    else
      local card = Fk:cloneCard("slash")
      card:addSubcards(cards)
      card.skillName = erdao.name

      if player:prohibitUse(card) then return nil end

      if not tos[1] then tos = { tos } end
      for i = #tos, 1, -1 do
        local p = tos[i]
        if player:isProhibited(p, card) then
          table.remove(tos, i)
        end
      end

      if #tos == 0 then return nil end

      local use = { ---@type UseCardDataSpec
        from = player,
        tos = tos,
        card = card,
        additionalDamage = -1
      }
      room:useCard(use)
    end
  end
})

Fk:loadTranslationTable{
  ["th_erdao"] = "二刀",
  [":th_erdao"] = "准备阶段，你可以获得一名角色场上的一张牌，然后将此牌当伤害-1的【杀】对其使用（若该角色为你则改为将此牌当【杀】使用）。",

  ["#th_erdao-choose"] = "二刀：你可以获得一名角色场上的一张牌并将此牌当伤害-1的【杀】对其使用（若该角色为你则改为将此牌当【杀】使用）",
  ["#th_erdao-slash"] = "二刀：你须将 %arg 当【杀】使用",
}

return erdao